package com.yiren.dbaa.module.practice_set.carmodel.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.PointExBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelCollectContract;
import com.yiren.dbaa.util.JtsUtils;

import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/12/7
 */
public class CarModelCollectModel implements CarModelCollectContract.Model {

    private Activity activity;
    private JtsUtils jtsUtils;
    private Gson gs;

    public CarModelCollectModel(Activity activity) {
        this.activity = activity;
        jtsUtils = new JtsUtils();
        gs = new Gson();
    }

    @Override
    public void fixOtherModelPoint(List<PointExBean> carModelPoints, GpsInfoBean gps) {
        //原点
        PointExBean originPointEx = carModelPoints.get(0);
        if (null != originPointEx) {
            for (int i = 1; i < carModelPoints.size(); i++) {
                PointExBean pointEx = carModelPoints.get(i);
                if (null == pointEx) {
                    continue;
                }
                pointEx.setPoint(pointEx.getX() - originPointEx.getX(), pointEx.getY() - originPointEx.getY());
                pointEx.setAngle(pointEx.getAngle() - gps.getAzimuth());
                pointEx.fixXY();
                carModelPoints.set(i, pointEx);
            }
        }
    }

    @Override
    public void fixPointByRadius(List<PointExBean> carModelPoints, float radius, CommonCallBack callBack) {
        PointExBean originPointEx = carModelPoints.get(0);
        PointExBean onePointEx = carModelPoints.get(1);
        if (null != originPointEx && null != onePointEx) {
            for (int i = 1; i <= 24; i++) {
                PointExBean pointEx = carModelPoints.get(i);
                if (null == pointEx) {
                    continue;
                }
                double[] adjustPoint = jtsUtils.getAdjustPoint(originPointEx, onePointEx, pointEx, radius);
                float x = pointEx.getX();
                float y = pointEx.getY();
                if (pointEx.getX() > 0) {  //在0点左边的点
                    x -= adjustPoint[0];
                } else if (pointEx.getX() < 0) {
                    x += adjustPoint[0];
                }
                if (pointEx.getY() > 0) {
                    y -= adjustPoint[1];
                } else if (pointEx.getY() < 0) {
                    y += adjustPoint[1];
                }
                pointEx.setPoint(x, y);
            }
            callBack.complete();
        }
    }

    @Override
    public void saveCarModel(CarModelEntity carModel, List<PointExBean> carModelPoints) {
        carModel.setPoints(gs.toJson(carModelPoints));
        UserDbController.getInstance(false).update(carModel);
    }
}
